package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.entity.response.ReturnBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class DADebugProductsActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etlOTID)
    FreeUI_ClearEditText etlOTID;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.etkz)
    FreeUI_ClearEditText liaohe;
    @BindView(R.id.etpj)
    FreeUI_ClearEditText qty;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_datjl;
    }

    @Override
    protected void initView() {
        tvTitle.setText("调机料");
        setMainLayout(llMain);

        tvBaseSubmit.setVisibility(View.GONE);

        etlOTID.requestFocus();
        initEdit(etlOTID, s -> {
            OrderRequest();
        });
    }

    private void  OrderRequest(){
        String lotid = etlOTID.getText().toString();
        if (lotid.equals("")){
            showToast("订单编号不能为空",false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    tvTitle.setText("调机料 - " + response.body().getResponse().getHeader().getRESULTMESSAGE());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(),"DaCsEqp");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("OrderNo", lotid);
        map.addProperty("ActionName", "chaxun");
        NetUtils.requestPostCPC("CPC.DaCsEqp","DaCsEqp",this,map,callBack);
    }

    @Override
    protected void initData() {

    }

    private void  SubmitRequest(String ActionName){
        String lotid = etlOTID.getText().toString();
        if (lotid.equals("")){
            showToast("订单编号不能为空",false);
            return;
        }

        String eqp = etEqpWorkID.getText().toString();
        if (eqp.equals("")){
            showToast("设备号或者库位号不能为空",false);
            return;
        }
        String qtyText = qty.getText().toString();

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    showToast(response.body().getResponse().getHeader().getRESULTMESSAGE(),true);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(),"DaCsEqp");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("OrderNo", lotid);
        map.addProperty("Eqp", eqp);
        map.addProperty("MaterialBox", getText(liaohe));
        map.addProperty("Qty", qtyText);
        map.addProperty("ActionName", ActionName);
        NetUtils.requestPostCPC("CPC.DaCsEqp","DaCsEqp",this,map,callBack);
    }

    @OnClick({R.id.tvBaseSubmit, R.id.btbaogong, R.id.btxiaohao, R.id.btkuwei})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                break;
            case R.id.btbaogong:
                if(actionControl("Workreport")) return;
                SubmitRequest("baogong");
                break;
            case R.id.btxiaohao:
                if(actionControl("Consume")) return;
                SubmitRequest("xiaohao");
                break;
            case R.id.btkuwei:
                if(actionControl("Warehouse")) return;
                SubmitRequest("kuwei");
                break;
        }

    }

}
